﻿package com.zxmclasslibrarie.location;

import android.content.Context;
import android.telephony.CellLocation;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;


/**
 * newwork定位 获取基站信息
 * 
 * @author Administrator
 * 
 */
public class CellLocationInfo {
	private static final String TAG = "CellLocationInfo";
	private static CellLocationInfo instance = null;
	private Context mContext = null;

	private boolean flag;

	private CellLocation location;
	private GsmCellLocation gsmLocation;
	private CdmaCellLocation cdmaLocation;

	private String MCCMNC = "";
	
	/**
	 * 基站数据状态
	 * 0 成功获得基站
	 * 1 无法获得基站
	 */
	private String cellResult = "0";

	private CellLocationInfo(Context context) {
		this.mContext = context;
		init();
	}

	public synchronized static CellLocationInfo getInstance(Context context) {
		if (null == instance) {
			instance = new CellLocationInfo(context);
		}
		return instance;
	}

	/**
	 * 初始化
	 */
	private void init() {
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		if (tm!=null) {
			location = tm.getCellLocation();
			MCCMNC = tm.getNetworkOperator();
			if (location != null) {
				if (location instanceof GsmCellLocation) {
					gsmLocation = (GsmCellLocation) location;
					flag = true;
				} else if (location instanceof CdmaCellLocation) {
					cdmaLocation = (CdmaCellLocation) location;
					flag = false;
				}
			}
		}
	}

	/**
	 * 获取location
	 * 
	 * @return
	 */
	public CellLocation getLocation() {
		if (flag) {
			return gsmLocation;
		} else {
			return cdmaLocation;
		}
	}

	/**
	 * 获取基站网络代码
	 * 
	 * @return
	 */
	public String getMNC() {
		if (flag) {
			if (null != MCCMNC && MCCMNC.length() > 3)
				return MCCMNC.substring(3);
			else
				return "0";
		} else {
			return String.valueOf(cdmaLocation.getSystemId());

		}
	}

	/**
	 * 获取基站区域代码
	 * 
	 * @return
	 */
	public String getLAC() {
		if (flag) {
			return String.valueOf(gsmLocation.getLac());
		} else {
			return String.valueOf(cdmaLocation.getNetworkId());
		}
	}

	/**
	 * 获取基站编号
	 * 
	 * @return
	 */
	public String getCellId() {
		if (flag) {
			return String.valueOf(gsmLocation.getCid());
		} else {
			return String.valueOf(cdmaLocation.getBaseStationId());
		}
	}

	/**
	 * 获取国家网络代码
	 * 
	 * @return
	 */
	public String getMCCMNC() {
		return MCCMNC;
	}
	
	public String getCellResult() {
		if ("-1".equals(getLAC())) {
			cellResult = "1";
		}
		return cellResult;
	}

}